File Upload in Node js using Multer

File Upload in Node js using Multer

File Upload in Node js using Multer is an important feature for any web application. Multer is a Node.js or express middleware, that makes it easy to handle multipart/form-data when your users upload files. When a website user uploads a file to a server, it is usually submitted via a form and encoded as multipart/form-data.

As I already said, Multer is Middleware for Express. Middleware is a piece of software which connects various applications or components of the software. Middleware runs in Express and sends incoming requests to the server. In our case, when uploading files Multer works as a helper.

In this tutorial, I’ll show you the basic procedure of File Upload in Node js using Multer.

Also Read: Drag and Drop File Upload using Dropzone JS, PHP & MySQL

File Upload in Node js using Multer

REQUIREMENTS

Project Setup

For this project we’ll be using the Node Express framework. You will, of course, need to have Node installed.

Watch: How to install Nodejs tutorials in hindi

Create a directory for our project

Move into the directory

create a .json file that manages all the dependencies for our application

Now, install Multer, Express, and the other necessary dependencies

Now create an app.js file

in app.js, we will initialize module, create an Express app, and create a server for connecting to browsers.

Create your project routing file routes/upload.js

Multer Configuration

Add the multer object to your routing file

We will upload the file in public/uploads directory. So our multer will contain the following configuration:

Multer Storage, File size Limits and file Filter configuration

Now the multer is ready to upload the file in public/uploads folder.

Create a routes/index.html file

Write all the code that will be served to the client to upload file.

App Routing

Now we will add the multer configuration in the routing file and add a post routing.

Copy and paste below code in routing file routes/upload.js

Now open your app.js file and search var app = express();

copy and paste below code after var app = express(); to set route

So now preview complete code of file:

app.js

routes/upload.js

Now our code is ready for test.

Now open your terminal and run below command

After running node app.js. You will get message:

node app.js
Server started on port 3000

Now open http://localhost:3000/ in your browser

File Upload in Nodejs using Multer

Now select your image file and click on upload button. After successfully upload you will get following response on browser.

File Upload in Node.js using Multer

Conclusion:

I hope you found this tutorial helpful for your project. File Upload in Node js using Multer is an important feature for any web application. So Here we are, completed the basic file uploading using multer in node.js and express.js

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Related posts